
/*
 * Copyright 2013 Wajdi Hh "wajdihh@gmail.com" .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hh.ui.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.hh.droid.R;
import com.hh.listeners.OnClearActionListener;
import com.hh.ui.UiUtility;

/**
 * 
 * A custom  EditText widget with search and clear icon
 * 
 * @author WajdiHh : Last modification : 23/05/2013
 *
 */

public class UiSearchableEditText extends RelativeLayout implements OnClickListener {

	private Button _mBtnSearch;
	private Button _mBtnClear;
	private EditText _mEdittext;

	private int _mMarginLeft;
	private int _mMarginBottom;
	private OnClearActionListener _mListener;
	
	public UiSearchableEditText(Context pContext, AttributeSet pAttributes) {
		super(pContext, pAttributes);
		
		float pDpValueMarginLeft=getContext().getResources().getDimension(R.dimen.marginLeft_uiEditText);		
		_mMarginLeft=UiUtility.getPxFromDp(getContext(), pDpValueMarginLeft);

		float pDpValuemMarginBottom=getContext().getResources().getDimension(R.dimen.marginBottom_uiEditText);		
		_mMarginBottom=UiUtility.getPxFromDp(getContext(), pDpValuemMarginBottom);
		
		prepareLayout(pContext);
	}
	/**
	 * <br>
	 * Sets text color
	 * 
	 * @author WajdiHh
	 * @param pColor
	 */
	public void setTextColor(int pColor){
		_mEdittext.setTextColor(pColor);
	}
	
	/**
	 * <br>
	 * Sets text size
	 * 
	 * @author WajdiHh
	 * @param pTextSize
	 */
	public void setTextSize(int pTextSize){
		_mEdittext.setTextSize(pTextSize);
	}
	/**
	 * <br>
	 * Sets the text to be displayed when the text of the TextView is empty
	 * 
	 * @author WajdiHh
	 * @param pHint : chars sequences
	 */
	public void setHint(String pHint){
		_mEdittext.setHint(pHint);
	}
	/**
	 * <br>
	 * Sets the text to be displayed when the text of the TextView is empty
	 * 
	 * @author WajdiHh
	 * @param pStringRes : resource string id
	 */
	public void setHint(int pStringRes){
		_mEdittext.setHint(getContext().getResources().getString(pStringRes));
	}
	/**
	 * <br>
	 * set Text
	 * 
	 * @author WajdiHh
	 * @param pText
	 */
	public void setText(String pText){
		_mEdittext.setText(pText);
	}
	
	/**
	 * <br>
	 * Return the editText of the customized view 
	 * 
	 * @author WajdiHh
	 * @return EditText
	 */
	public EditText getEditText(){
		return _mEdittext;
	}
	/**
	 * <br>
	 * Return the text content of the customized view 
	 * 
	 * @author WajdiHh
	 * @return text: String
	 */
	public String getTextToString(){
		return _mEdittext.getText().toString();
	}

	/**
	 * <br>
	 * Return the editable object
	 * 
	 * @author WajdiHh
	 * @return text: String
	 */
	public Editable getText(){
		return _mEdittext.getText();
	}

	/**
	 * <br>
	 * set the visibility of search button : true = visible , false : gone
	 * 
	 * @author WajdiHh
	 * @param pIsVisible
	 */
	public void setVisibilityOfSearchButton(boolean pIsVisible){

		if(pIsVisible)
			_mBtnSearch.setVisibility(View.VISIBLE);
		else
			_mBtnSearch.setVisibility(View.GONE);
	}
	/**
	 * <br>
	 * set the visibility of clear button : true = visible , false : gone
	 * 
	 * @author WajdiHh
	 * @param pIsVisible
	 */
	public void setVisibilityOfClearButton(boolean pIsVisible){

		if(pIsVisible)
			_mBtnClear.setVisibility(View.VISIBLE);
		else
			_mBtnClear.setVisibility(View.GONE);
	}
	
	/**
	 * <br>
	 * Set search button background
	 * 
	 * @author WajdiHh
	 * @param pDrawbleRes : the resource ID of the selected drawable
	 */
	public void setBackgroundSearchButton(int pDrawbleRes){
		
		Drawable d=getContext().getResources().getDrawable(pDrawbleRes);
		_mBtnSearch.setBackgroundDrawable(d);
	}
	
	/**
	 * <br>
	 * Set clear button background
	 * 
	 * @author WajdiHh
	 * @param pDrawbleRes : the resource ID of the selected drawable
	 */
	public void setBackgroundClearButton(int pDrawbleRes){
		
		Drawable d=getContext().getResources().getDrawable(pDrawbleRes);
		_mBtnClear.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
	}

	/**
	 * <br>
	 * Register a callback to be invoked when the clear button is clicked. 
	 * 	 
	 * @author WajdiHh
	 * @param pListener : onClearActionListener
	 * @see onClearActionListener
	 */
	public void setOnClearActionListener(OnClearActionListener pListener){
		_mListener=pListener;
	}
	
	
	private void prepareLayout(Context pContext) {

		LayoutInflater lInflater = (LayoutInflater)pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		lInflater.inflate(R.layout.ui_searchable_editext, this);

		_mBtnSearch = (Button) findViewById(R.id.UiEd_BtnSearch);
		_mBtnClear = (Button)findViewById(R.id.UiEd_BtnClear);
		_mEdittext = (EditText)findViewById(R.id.UiEd_Ed);

		_mBtnClear.setOnClickListener(this);
		_mEdittext.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

				if(s.toString().length()==0){

					_mEdittext.setPadding(_mMarginLeft, 0, 0, _mMarginBottom);
					_mBtnSearch.setVisibility(View.VISIBLE);
					_mBtnClear.setVisibility(View.GONE);
				}else{
					_mEdittext.setPadding(2*_mMarginBottom, 0, 0, _mMarginBottom);
					if(_mBtnSearch.getVisibility()==View.VISIBLE)
						_mBtnSearch.setVisibility(View.GONE);

					if(_mBtnClear.getVisibility()==View.GONE)
						_mBtnClear.setVisibility(View.VISIBLE);
				}

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {

			}			
			@Override
			public void afterTextChanged(Editable s) {

			}
		});

	}

	@Override
	public void onClick(View v) {
		_mEdittext.setText("");
		if(_mListener!=null)
			_mListener.onClearAction(v);
	}

}
